<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      //apply()方法使用给定的this值调用一个函数，并以数组的形式提供参数
      let numbers = [5, 6, 2, 3, 7];
      let max1 = Math.max.apply(null, numbers);
      document.write(max1, "<br>");
      //call()和apply()的区别
      //call()方法单独接受参数
      //apply()将方法参数作为数组

      let max = Math.max.apply(null, numbers);
      let min = Math.min(...numbers);

      for (let i = 0; i < numbers.length; i++) {
        if (numbers[i] > max) {
          max = numbers[i];
        }
        if (numbers[i] < min) {
          min = numbers[i];
        }
      }

      document.write(min, "<br>", max);
      //使用apply()调用函数而不指定参数
      var name = "Seagull";
      var obj = {
        name: "Dolphin",
      }; // 自定义对象

      function display() {
        document.write("<br>", this.name);
      }

      display(); // 直接调用：this指向window → 输出 "Seagull"
      display.apply(obj); // apply指定this指向obj → 输出 "Dolphin"
    </script>
  </body>
</html>
